<script setup lang="ts">
import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
// 导入post接口
import { registerAPI } from '@/apis/registerAPI'





const ruleFormRef = ref<FormInstance>()

const validatepassword = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else {
    callback()
  }
}

const ruleForm = reactive({
  nickname: '',
  password: '',
})

const rules = reactive<FormRules<typeof ruleForm>>({
  password: [{ validator: validatepassword, trigger: 'blur' }],
})

const submitForm = (formEl: FormInstance | undefined) => {
  // 解构上传数据
  const { nickname, password } = ruleForm
  if (!formEl) return
  formEl.validate(async (valid) => {//async
    if (valid) {
      // await XXX({{password, checkpassword}})
      await registerAPI({ nickname, password })
      console.log('submit!')
    } else {
      console.log('error submit!')
      return false
    }
  })
}



</script>
<template>
  <!-- 头部 -->
  <div class="header">
    <div class="logo"></div>
    <div class="return" @click="$router.push('/login')">点击登入</div>
  </div>
  <!-- 注册区 -->
  <div class="register">
    <el-form ref="ruleFormRef" :model="ruleForm" status-icon :rules="rules" label-width="5vw" class="demo-ruleForm"
      label-position="right" size="large">
      <div class="register-box">
        <el-form-item prop="nickname" label="账户">
          <el-input v-model="ruleForm.nickname" class="nickname" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password" type="password" autocomplete="off" class="pass" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)" class="zuce">注册</el-button>
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>


<style lang="scss" scoped>
// 头部logo
.header {
  margin: 0 auto;
  width: 70vw;
  height: 8vw;
  background-color: #fff;

  .logo {
    float: left;
    width: 8vw;
    height: 8vw;
    background-color: red;
    background: url("@/assets/images/01-logo.png") no-repeat center;
    background-size: contain;
    margin-left: 20vw;
  }

  .return {
    float: right;
    line-height: 8vw;
    font-size: 1.5vw;
    margin-right: 20vw;
    color: #626d85;

    &:hover {
      opacity: 0.8;
    }
  }
}

// 注册区域

.register {
  margin: 0 auto;
  position: relative;
  width: 70vw;
  height: 40vw;
  background: url("@/assets/images/02-background.png") no-repeat center;
  background-size: 100vw 40vw;

  .demo-ruleForm {
    width: 30vw;
    height: 20vw;
    position: absolute;
    top: 10vw;
    right: 20vw;
    border-radius: 2vw;
    border: 2px solid #eae8e5;
    transition: all .5s;
    background-color: #fff;

    &:hover {

      transform: translate3d(0, -3px, 0);
      box-shadow: 0 3px 8px rgb(0 0 0 / 20%);
    }

    .register-box {
      margin: 0 auto;
      width: 20vw;
      margin-top: 4vw;

      .zuce {
        float: left;
        width: 15vw;
      }


    }
  }
}
</style>